<?php

include_once 'events.php';

  /**
   * Remove spaces
   * Turn ' and ' to +s
   */
function getDirName($name) {
  $res = $name;
  $res = preg_replace('/\s+/','',$res);
  $res = preg_replace('/ and /','+',$res);
  return $res;
  }

function isInCityDir() {
  return file_exists('data.xml');
}

/**
 * Returns the proper name given a dir -- e.g. LosAngeles -> "Los
 * Angeles".  This is basically taking from camel-case and replacing
 * the upper case letters with spaces before them.
 */
function getTitleFromDir($dir) {
  $result = '';
  $arr = preg_split('//',$dir);
  foreach ($arr as $c) {
    if (isupper($c)) $result .= ' ';
    if ($c == '+') {
      $result .= 'and';
    } else if (preg_match('/\w/', $c) || preg_match('/\s/', $c)) {
      $result .= $c;
    }
  }
  return $result;
}

/**
 * Returns <code>TRUE</code> if <code>$c</code> is uppercase.
 */
function isupper($c) {
  return strtoupper($c) == $c;
}

/**
 * Returns <code>TRUE</code> if <code>$c</code> is lowercase.
 */
function islower($c) {
  return strtolower($c) == $c;
}

/**
 * Links to the directory pointed to by the $name.  If $convertName is
 * TRUE (it's FALSE by default) we convert the directory name that
 * will be displayed in the <code>innerHTML</code>.
 */
function linkTo($name,$convertName=FALSE) {
  $title = $convertName ? getTitleFromDir($name) : $name;
  return '<a href="' . getDirName($name) . '">' . $title . '</a>';
}

function ahref($href,$name) {
  return '<a href="' . BASE . $href . '">' . $name . '</a>';
}

function br() {
  return '<br/>';
}

function isNormal() {
  return !(isIPhone() || isTable() || isAlphabetical() || isSearch());
}

function isIPhone() {
  if (isSession('iphone')) return true;
  $ua = $_SERVER['HTTP_USER_AGENT'];
  return preg_match('/iPhone/',$ua);
}

function isSearch() {
  return isSession('search');
}

function isTable() {
  return isSession('table');
}

function isAlphabetical() {
  return isSession('alphabetical');
}

function isList() {
  if (isset($_REQUEST['normal'])) return TRUE;
  if (isset($_SESSION['normal'])) return TRUE;
  return false;
}

/**
 * Returns an array dir names startin from $dir or '.' if nothing is
 * passed in.
 */
function list_dirs($dir='.') {
  $dirs = array();
  if(is_dir($dir)) {
    if($handle = opendir($dir)) {
      while(($file = readdir($handle)) !== false) {
        if ($file != '.' && $file != '..' && is_dir($file)) {
          $dirs[]= $file;
        }
      }
      closedir($handle);
    }
  }
  return $dirs;
}

/**
 * Returns true if we should use the map.
 */
function useMap() {
  return defined('GOOGLE_MAPS_KEY');
}

static $_days2abbrevs = array(
                              'Monday'    => 'MON',
                              'Tuesday'   => 'TUE',
                              'Wednesday' => 'WED',
                              'Thursday'  => 'THU',
                              'Friday'    => 'FRI',             
                              'Saturday'  => 'SAT',
                              'Sunday'    => 'SUN'
                              );
function abbrevFromDay($day) {
  return $_days2abbrevs[$day];
}

static $_abbrevs2days = array(
                              'MON' => 'Monday',
                              'TUE' => 'Tuesday',
                              'WED' => 'Wednesday',
                              'THU' => 'Thursday',
                              'FRI' => 'Friday',
                              'SAT' => 'Saturday',
                              'SUN' => 'Sunday',
                      );
function dayFromAbbrev($abbrev) {
  return $_abbrevs2days[$abbrev];
}

function removeHTML($s,$replacement='') {
  return preg_replace('/<[^>]+>/',$replacement,$s);
}

function phpSelf() {
  return preg_replace('/index.php/','',$_SERVER['PHP_SELF']);
}

function underLink($noLink,$relUrl,$name) {
  echo ' &nbsp; ';
  if ($noLink) {
    echo '<em>' . $name . '</em>';
  } else {
    echo '<a href="' . phpSelf();
      if ($relUrl) echo '?' . $relUrl;
      echo '">' . $name . '</a>';
  }
}

function printEvents($events,$id,$name=NULL) {
  if (!$name) {
    $name = $events[0]->getTitle();
  }
  if (useMap()) {
    $divId = 'div_' . $id;
    $frameId = 'iframe_' . $id;
    $e = $events[0];
    $mapLink = ' [<a id="' . $id . '" class="mapLink" onclick="toggleMap(\'' . BASE . '\',\'' 
      . $id . '\',\'' . $divId . '\',\'' . $frameId . '\',' 
      . $e->getLatitude() . ',' . $e->getLongitude() . ')" href="#">map</a>]';
    echo $e->asHTML($name,$mapLink);
    echo '<div id="' . $divId . '" style="display:none">';
    echo '<iframe frameborder="0" border="0" class="smallMap" style="height:300px;width:400px" id="' 
      . $frameId . '"></iframe>';
    echo '</div>';
  }
  echo '<ul>';
  foreach ($events as $e) {
    echo '<li>';
    echo $e->getDay() . ' @ ' . $e->getTime();
    echo '</li>';
  }
  echo '</ul>';
  echo '<br/>';
  echo "\n";
}

?>